package com.wyvoid.forum.service.impl;

import com.wyvoid.forum.service.IUserService;
import com.wyvoid.forum.domain.User;
import com.wyvoid.forum.domain.UserProfile;
import com.wyvoid.forum.dao.IUserDAO;
import com.wyvoid.forum.dao.IUserProfileDAO;
import com.wyvoid.forum.exception.user.UsernameNotFoundException;
import com.wyvoid.forum.exception.user.UserExistsException;
import com.wyvoid.forum.exception.user.PasswordFailureException;
import com.wyvoid.forum.exception.admin.AdminLoginFailureException;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import com.wyvoid.core.persistent.Pagination;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.dao.DataIntegrityViolationException;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-13
 * Time: 12:05:10
 */
public class UserService implements IUserService {
    private IUserDAO userDAO;
    private IUserProfileDAO userProfileDAO;

    public void setUserDAO(IUserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setUserProfileDAO(IUserProfileDAO userProfileDAO) {
        this.userProfileDAO = userProfileDAO;
    }

    @Transactional
    public void createUser(User user, UserProfile userProfile) throws UserExistsException {
        try {
            Date now = new Date();
            user.setLastLoginTime(now);
            user.setRegisterTime(now);
            userDAO.create(user);
            userProfile.setUser(user);
            userProfileDAO.create(userProfile);
        } catch (DataIntegrityViolationException e) {
            throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
        }
    }

    public User getUser(long userId) {
        return userDAO.getById(userId);
    }

    public void updateUser(User user) {
        userDAO.update(user);
    }

    public void updateUserAndProfile(User user, UserProfile userProfile) {
        userDAO.update(user);
        userProfileDAO.update(userProfile);
    }

    public User getUser(String userName) {
        return userDAO.getUser(userName);
    }

    public long countUser() {
        return userDAO.countUser();
    }

    public User getNewestUser() {
        return userDAO.getNewestUser();
    }

    public User loginUser(String username, String password) throws UsernameNotFoundException, PasswordFailureException {
        User user = userDAO.getUser(username);
        if (user == null) {
            throw new UsernameNotFoundException();
        } else if (!StringUtils.equals(password, user.getPassword())) {
            throw new PasswordFailureException();
        } else {
            return user;
        }
    }

    public UserProfile getUserProfile(long userId) {
        return userProfileDAO.getUserProfile(userId);
    }

    public UserProfile getUserProfileById(long userProfileId) {
        return userProfileDAO.getById(userProfileId);
    }

    public UserProfile getUserProfileByUserName(String userName) {
        return userProfileDAO.getUserProfileByUserName(userName);
    }

    public HibernatePage getAllUser(Pagination pagination) {
        return userProfileDAO.getAllUser(pagination);
    }

    public HibernatePage getSearchUser(String strUserKey, Pagination pagination) {
        return userProfileDAO.getSearchUser(strUserKey, pagination);
    }

    public HibernatePage getSearchUserName(String strUserName, Pagination pagination) {
        return userProfileDAO.getSearchUserName(strUserName, pagination);
    }

    public User authLogin(String username, String password) throws AdminLoginFailureException{
        User user = userDAO.getUser(username);
        if (user == null) {
            throw new AdminLoginFailureException("admin login failure");
        } else if (user.getAdminId()<=0||!StringUtils.equals(password, user.getPassword())) {
            throw new AdminLoginFailureException("admin login failure");
        } else {
            return user;
        }
    }
}
